﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class TransferDA : ITransferDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string FROM_OWNER_ID = "@FROM_OWNER_ID";
        private string GEN_IS_IN_CHARGES = "@GEN_IS_IN_CHARGES";
        private string GEN_OT_CHARGES = "@GEN_OT_CHARGES";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string OPEN_QTY = "@OPEN_QTY";
        private string REMARK = "@REMARK";
        private string SQL_CHECK_TRANSFER_ID_UNIQUE = " SELECT COUNT(1) FROM @_@TRANSFER WHERE TRANSFER_ID = @TRANSFER_ID ";
        private string SQL_DELETE_TRANSFER = " DELETE FROM @_@TRANSFER WHERE TRANSFER_ID = @TRANSFER_ID ";
        private string SQL_INSERT_TRANSFER = " INSERT INTO @_@TRANSFER ( WH_ID, TRANSFER_ID, FROM_OWNER_ID, TO_OWNER_ID, TRANSFER_TYPE, OPEN_QTY, TRANSFER_DATE, GEN_OT_CHARGES, GEN_IS_IN_CHARGES, STATUS, UDF1, UDF2, UDF3, UDF4, UDF5, REMARK, IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @TRANSFER_ID, @FROM_OWNER_ID, @TO_OWNER_ID, @TRANSFER_TYPE, @OPEN_QTY, @TRANSFER_DATE, @GEN_OT_CHARGES, @GEN_IS_IN_CHARGES, @STATUS, @UDF1, @UDF2, @UDF3, @UDF4, @UDF5, @REMARK, @IS_ACTIVE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_TRANSFER = " SELECT  ROWID, WH_ID, TRANSFER_ID, FROM_OWNER_ID, TO_OWNER_ID, TRANSFER_TYPE, OPEN_QTY, TRANSFER_DATE, GEN_OT_CHARGES, GEN_IS_IN_CHARGES, STATUS, UDF1, UDF2, UDF3, UDF4, UDF5, REMARK, IS_ACTIVE, TS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@TRANSFER ";
        private string SQL_UPDATE_TRANSFER = " UPDATE @_@TRANSFER SET WH_ID = @WH_ID, FROM_OWNER_ID = @FROM_OWNER_ID, TO_OWNER_ID = @TO_OWNER_ID, TRANSFER_TYPE = @TRANSFER_TYPE, OPEN_QTY = @OPEN_QTY, TRANSFER_DATE = @TRANSFER_DATE, GEN_OT_CHARGES = @GEN_OT_CHARGES, GEN_IS_IN_CHARGES = @GEN_IS_IN_CHARGES, STATUS = @STATUS, UDF1 = @UDF1, UDF2 = @UDF2, UDF3 = @UDF3, UDF4 = @UDF4, UDF5 = @UDF5, REMARK = @REMARK, IS_ACTIVE = @IS_ACTIVE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE TRANSFER_ID = @TRANSFER_ID  ";
        private string SQL_UPDATE_TRANSFER_STATUS = " UPDATE @_@TRANSFER SET STATUS = @STATUS , UPDATED_BY = @UPDATED_BY WHERE TRANSFER_ID = @TRANSFER_ID ";
        private string STATUS = "@STATUS";
        private string TO_OWNER_ID = "@TO_OWNER_ID";
        private string TRANSFER_DATE = "@TRANSFER_DATE";
        private string TRANSFER_ID = "@TRANSFER_ID";
        private string TRANSFER_TYPE = "@TRANSFER_TYPE";
        private string TS = "@TS";
        private string UDF1 = "@UDF1";
        private string UDF2 = "@UDF2";
        private string UDF3 = "@UDF3";
        private string UDF4 = "@UDF4";
        private string UDF5 = "@UDF5";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public void ChangeTransferStatus(DataBase dataBase, DbTransaction tran, string transferID, string transferStatus, string updatedBy, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@STATUS", transferStatus), new SqlParameter("@UPDATED_BY", updatedBy), new SqlParameter("@TRANSFER_ID", transferID) };
            dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_TRANSFER_STATUS.Replace("@_@", whLoginID), paras);
        }

        public bool CheckTransferIDUnique(string transferID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@TRANSFER_ID", transferID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_TRANSFER_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckTransferIDUnique(DataBase dataBase, DbTransaction tran, string transferID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@TRANSFER_ID", transferID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_TRANSFER_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteTransfer(List<string> transferID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < transferID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@TRANSFER_ID", transferID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_TRANSFER.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteTransfer(DataBase dataBase, DbTransaction tran, List<string> transferID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < transferID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@TRANSFER_ID", transferID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_TRANSFER.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllTransfer(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_TRANSFER.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllTransfer(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_TRANSFER.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public TransferInfo GetTransferByID(string transferID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_TRANSFER.Replace("@_@", whLoginID) + " WHERE TRANSFER_ID = @TRANSFER_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@TRANSFER_ID", transferID) };
            TransferInfo transferInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    DateTime CS00003;
                    transferInfo = new TransferInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["TRANSFER_ID"].ToString(), reader["FROM_OWNER_ID"].ToString(), reader["TO_OWNER_ID"].ToString(), reader["TRANSFER_TYPE"].ToString(), (reader["OPEN_QTY"].ToString() != "") ? decimal.Parse(reader["OPEN_QTY"].ToString()) : 0M, (reader["TRANSFER_DATE"].ToString() != "") ? DateTime.Parse(reader["TRANSFER_DATE"].ToString()) : (CS00003 = new DateTime()), reader["GEN_OT_CHARGES"].ToString(), reader["GEN_IS_IN_CHARGES"].ToString(), reader["STATUS"].ToString(), reader["UDF1"].ToString(), reader["UDF2"].ToString(), reader["UDF3"].ToString(), reader["UDF4"].ToString(), reader["UDF5"].ToString(), reader["REMARK"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return transferInfo;
        }

        public TransferInfo GetTransferByID(DataBase dataBase, DbTransaction tran, string transferID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_TRANSFER.Replace("@_@", whLoginID) + " WHERE TRANSFER_ID = @TRANSFER_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@TRANSFER_ID", transferID) };
            TransferInfo transferInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                DateTime CS00003;
                transferInfo = new TransferInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["TRANSFER_ID"].ToString(), reader["FROM_OWNER_ID"].ToString(), reader["TO_OWNER_ID"].ToString(), reader["TRANSFER_TYPE"].ToString(), (reader["OPEN_QTY"].ToString() != "") ? decimal.Parse(reader["OPEN_QTY"].ToString()) : 0M, (reader["TRANSFER_DATE"].ToString() != "") ? DateTime.Parse(reader["TRANSFER_DATE"].ToString()) : (CS00003 = new DateTime()), reader["GEN_OT_CHARGES"].ToString(), reader["GEN_IS_IN_CHARGES"].ToString(), reader["STATUS"].ToString(), reader["UDF1"].ToString(), reader["UDF2"].ToString(), reader["UDF3"].ToString(), reader["UDF4"].ToString(), reader["UDF5"].ToString(), reader["REMARK"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return transferInfo;
        }

        public DataSet GetTransferByQueryList(List<string> sqlWhere, TransferQueryEntity transferQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_TRANSFER.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!transferQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, transferQuery.CurrentPage, transferQuery.PageSize, transferQuery.SortField, transferQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetTransferByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, TransferQueryEntity transferQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_TRANSFER.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!transferQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, transferQuery.CurrentPage, transferQuery.PageSize, transferQuery.SortField, transferQuery.SortDirection);
            }
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertTransfer(TransferInfo transferInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                transferInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_Transfer_Parameters(transferInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_TRANSFER.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertTransfer(DataBase dataBase, DbTransaction tran, TransferInfo transferInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                transferInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_Transfer_Parameters(transferInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_TRANSFER.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_Transfer_Parameters(TransferInfo transferInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { 
                new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.TRANSFER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.FROM_OWNER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.TO_OWNER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.TRANSFER_TYPE, SqlDbType.VarChar, 20), new SqlParameter(this.OPEN_QTY, SqlDbType.Decimal, 13), new SqlParameter(this.TRANSFER_DATE, SqlDbType.DateTime, 8), new SqlParameter(this.GEN_OT_CHARGES, SqlDbType.VarChar, 1), new SqlParameter(this.GEN_IS_IN_CHARGES, SqlDbType.VarChar, 1), new SqlParameter(this.STATUS, SqlDbType.VarChar, 20), new SqlParameter(this.UDF1, SqlDbType.VarChar, 30), new SqlParameter(this.UDF2, SqlDbType.VarChar, 30), new SqlParameter(this.UDF3, SqlDbType.VarChar, 30), new SqlParameter(this.UDF4, SqlDbType.VarChar, 30), new SqlParameter(this.UDF5, SqlDbType.VarChar, 30), new SqlParameter(this.REMARK, SqlDbType.VarChar, 0x3e8), 
                new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20)
             };
            if (!string.IsNullOrEmpty(transferInfo.WhID))
            {
                paramArray[0].Value = transferInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = transferInfo.TransferID;
            paramArray[2].Value = transferInfo.FromOwnerID;
            paramArray[3].Value = transferInfo.ToOwnerID;
            paramArray[4].Value = transferInfo.TransferType;
            paramArray[5].Value = transferInfo.OpenQty;
            paramArray[6].Value = transferInfo.TransferDate;
            paramArray[7].Value = transferInfo.GenOtCharges;
            paramArray[8].Value = transferInfo.GenIsInCharges;
            paramArray[9].Value = transferInfo.Status;
            if (!string.IsNullOrEmpty(transferInfo.Udf1))
            {
                paramArray[10].Value = transferInfo.Udf1;
            }
            else
            {
                paramArray[10].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(transferInfo.Udf2))
            {
                paramArray[11].Value = transferInfo.Udf2;
            }
            else
            {
                paramArray[11].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(transferInfo.Udf3))
            {
                paramArray[12].Value = transferInfo.Udf3;
            }
            else
            {
                paramArray[12].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(transferInfo.Udf4))
            {
                paramArray[13].Value = transferInfo.Udf4;
            }
            else
            {
                paramArray[13].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(transferInfo.Udf5))
            {
                paramArray[14].Value = transferInfo.Udf5;
            }
            else
            {
                paramArray[14].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(transferInfo.Remark))
            {
                paramArray[15].Value = transferInfo.Remark;
            }
            else
            {
                paramArray[15].Value = DBNull.Value;
            }
            paramArray[0x10].Value = transferInfo.IsActive;
            paramArray[0x11].Value = transferInfo.CreatedBy;
            paramArray[0x12].Value = transferInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateTransfer(TransferInfo transferInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                transferInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_Transfer_Parameters(transferInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_TRANSFER.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateTransfer(DataBase dataBase, DbTransaction tran, TransferInfo transferInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                transferInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_Transfer_Parameters(transferInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_TRANSFER.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

